package com.fastloan.utils.excelutil;

import com.fastloan.utils.DateTimeUtil;
import com.fastloan.utils.excelutil.model.BetweenDate;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yanwe
 *         createTime 2017-06-2017/6/9 14:48
 */
public class DateCheckUtil {

    public static boolean checkDateCross(List<BetweenDate> betweenDates){
        for (int i = 0; i < betweenDates.size(); i++) {
            BetweenDate targetDate = betweenDates.get(i);
            for (int j = i + 1; j < betweenDates.size(); j++) {
                BetweenDate betweenDate = betweenDates.get(j);
                if (checkDateCross(targetDate,betweenDate)){
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean checkDateCross(BetweenDate dateOne,BetweenDate dateTwo){
        LocalDateTime dateOneBeginDate = dateOne.getBeginDate();
        LocalDateTime dateOneEndDate = dateOne.getEndDate();
        LocalDateTime dateTwoBeginDate = dateTwo.getBeginDate();
        LocalDateTime dateTwoEndDate = dateTwo.getEndDate();
        if (dateOneBeginDate.isBefore(dateTwoBeginDate)){
            if (dateTwoBeginDate.isBefore(dateOneEndDate) || dateTwoBeginDate.isEqual(dateOneEndDate)){
                return true;
            }
        } else {
            if (dateOneBeginDate.isBefore(dateTwoEndDate) || dateOneBeginDate.isEqual(dateTwoEndDate)){
                return true;
            }
        }
        return false;
    }

    public static <T> List<BetweenDate> convert2BetweenDate(List<T> list, Function<T,Date> beginDateFunction
            , Function<T,Date> endDateFunction){
        return list.stream()
                .map(o -> new BetweenDate(DateTimeUtil.date2LocalDateTime(beginDateFunction.apply(o))
                        ,DateTimeUtil.date2LocalDateTime(endDateFunction.apply(o))))
                .collect(Collectors.toList());
    }
}
